extends Node


var unique_id: int = 0
var room_group_map: Dictionary ={}

func register_room(room:Room2D):
	var room_name = _generate_room_name()
	room.room_unique_name = room_name
	room_group_map[room_name] = room


func get_room_path_to_pos(node:Node2D,target_pos):
	var room:Room2D = find_node_room(node)
	if not is_instance_valid(room):
		return []

	var src_pos = node.global_position
	return room.get_nav_path(src_pos,target_pos)

func get_room_player(node: Node2D) -> Player:
	var room: Room2D = find_node_room(node)
	if room == null:
		return null
	
	var player_room = PlayManager.player.get_meta("Room", "NoRoom")
	if player_room != room.room_unique_name:
		return null
	return PlayManager.player

func find_node_room(node: Node2D) -> Room2D:
	var room_group_name: String= node.get_meta("Room","NoRoom")
	if room_group_name == "NoRoom":
		return null
	if not room_group_map.has(room_group_name):
		return null
	return room_group_map[room_group_name]

func get_room_name(room:Room2D):
	return room_group_map[room]

func clear():
	room_group_map.clear()

func _generate_room_name():
	unique_id=unique_id+1
	return "Room"+String.num_int64(unique_id)
